import {useNavigate} from "react-router-dom";
import {useEffect, useState} from "react";
import {message} from "antd";
import {getProjectInfoAPI, getProjectListAPI} from "@/apis/project.jsx";


const useGetProjectById = (id) => {
    const navigator = useNavigate();
    const [project, setProject] = useState({});
    useEffect(() => {
        const getOneProjectById = async () => {
            const res = await getProjectInfoAPI(id);
            if (res.code === 200) {
                setProject(res.data);
            }
            else if (res.code === 500) {
                message.error(res.msg);
                navigator('/backgroundManage/project')
            }
        }
        getOneProjectById().then()
    }, [])
    return project;
}

const useGetProjectList=(cur,pageSize)=>{
    const [projectList,setProjectList]=useState([]);
    useEffect(()=>{
        const getProjectList=async ()=>{
            const res=await getProjectListAPI(cur,pageSize);
            if (res.code===200){
                setProjectList(res.data);
            }
        }
        getProjectList().then()
    },[])

    return projectList;
}




export {
    useGetProjectById,useGetProjectList
}